if (NOT ZENO_BUILD_SHARED)
    message(FATAL_ERROR "ZENO_BUILD_SHARED must be ON when ZENO_WITH_Python is ON! Please specify: -DZENO_BUILD_SHARED:BOOL=ON")
endif()

file(GLOB_RECURSE sources CONFIGURE_DEPENDS *.cpp *.h)
target_sources(zeno PRIVATE ${sources})

set(ZENO_PYTHON_MODULE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Lib)
set(ZENO_PYTHON_HOME_DIR ${CMAKE_CURRENT_BINARY_DIR}/CPython)
set(ZENO_PYTHON_LIB_DIR ${ZENO_PYTHON_HOME_DIR}/lib)
if (WIN32)
    set(ZENO_PYTHON_DLL_FILE zeno.dll)
elseif (APPLE)
    set(ZENO_PYTHON_DLL_FILE libzeno.dylib)
else()
    set(ZENO_PYTHON_DLL_FILE libzeno.so)
endif()
configure_file(zeno_Python_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/confinc/zeno_Python_config.h @ONLY)
target_include_directories(zeno PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/confinc)
zeno_add_asset_dir(${ZENO_PYTHON_MODULE_DIR})
zeno_add_asset_dir(${ZENO_PYTHON_LIB_DIR})

if (WIN32)
    set(BUILD_LIBPYTHON_SHARED OFF CACHE BOOL "ZENOCIHOUPYTHON" FORCE)
    set(BUILD_EXTENSIONS_AS_BUILTIN OFF CACHE BOOL "ZENOCIHOUPYTHON" FORCE)
else()
    set(BUILD_LIBPYTHON_SHARED OFF CACHE BOOL "ZENOCIHOUPYTHON" FORCE)
    set(BUILD_EXTENSIONS_AS_BUILTIN ON CACHE BOOL "ZENOCIHOUPYTHON" FORCE)
endif()
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CPython/CMakeLists.txt)
    message(FATAL_ERROR "CPython submodule not found! Please run: git submodule update --init --recursive")
endif()
if (ZENO_OUT_TO_BIN)
    unset(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
    unset(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
    unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
    unset(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG)
    unset(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG)
    unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG)
    unset(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE)
    unset(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE)
    unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE)
endif()
add_subdirectory(CPython)

if (BUILD_LIBPYTHON_SHARED)
    target_link_libraries(zeno PRIVATE libpython3-shared)
else()
    target_link_libraries(zeno PRIVATE libpython-static)
endif()

#### BEGIN cihou python dapigu ####
add_dependencies(zeno python)
get_property(fuckers GLOBAL PROPERTY FUCKING_PYTHON_EXTENSION_TARGETS)
foreach (fucker IN ITEMS ${fuckers})
    if (TARGET ${fucker})
        add_dependencies(zeno ${fucker})
        message(STATUS "[+] ${fucker}")
    else()
        message(STATUS "[ ] ${fucker}")
    endif()
endforeach()
get_property(fuckconfig GLOBAL PROPERTY FUCK_PYTHON_SYSCONFIG_NIMABI)
get_property(fuckconfig2 GLOBAL PROPERTY FUCK_PYTHON_SYSCONFIG_NIMABI2)
get_property(fuckconfig3 GLOBAL PROPERTY FUCK_PYTHON_SYSCONFIG_NIMABI3)
if(UNIX)
    add_custom_target(fuckpythonnimabidesysconfig
        WORKING_DIRECTORY ${fuckconfig3}
        COMMENT "CIHOU PYTHON WDNMD DIAONIDAYE CONNIMAFIG"
        DEPENDS python
        COMMAND ${CMAKE_COMMAND} -E copy_if_different "${fuckconfig}" "${fuckconfig2}"
    )
    add_dependencies(zeno fuckpythonnimabidesysconfig)
endif()
##### END cihou python dapigu #####

target_include_directories(zeno PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}/Python-${PYTHON_VERSION}/Include   # Python.h (unix)
    ${CMAKE_CURRENT_BINARY_DIR}/CPython/bin                        # pyconfig.h (unix)
    ${CMAKE_CURRENT_SOURCE_DIR}/Python-${PYTHON_VERSION}/Include   # Python.h (win)
    ${CMAKE_CURRENT_BINARY_DIR}/Python-${PYTHON_VERSION}/PC        # pyconfig.h (win)
    )
